package com.gm.commonlib.views.dialog;

import static android.content.Context.INPUT_METHOD_SERVICE;
import static android.view.View.VISIBLE;

import android.app.Dialog;
import android.content.Context;
import android.text.InputFilter;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gm.commonlib.R;
import com.gm.commonlib.filters.InputFilterEmoji;
import com.gm.commonlib.filters.RegexConstant;
import com.gm.commonlib.filters.RegexInputFilter;
import com.gm.commonlib.util.viewUtils.KeyboardUtils;

import java.util.List;


public class InputDialog extends Dialog {
    private TextView mTitleTv;
    private EditText mInputDataEd;
    private Context context;
    private String title, hintText;
    private OnSubMitBtnClickListener listener;
    public TextView leftTv, rightTv;
    private String redHintNt;
    private int maxInputSize;

    private boolean mIsShowRedNt = false;
    private boolean mIsClickDimissDialog=false;
    private TextView edLeftTv;
    private TextView edRightTv;
    private HorizontalScrollView contentLinView;


    public interface OnSubMitBtnClickListener {
        void onSubMitBtnClick(String inputData,InputDialog inputDialog);
    }

    public InputDialog(Context context) {
        this(context, true);
    }

    public InputDialog(Context context, boolean canOutSideDissMiss) {
        super(context, R.style.alert_dialog);
        this.context = context;
        setCanceledOnTouchOutside(canOutSideDissMiss);
        init();
    }


    private void init() {
        setContentView(R.layout.input_dialog);
        leftTv = findViewById(R.id.leftTv);
        rightTv = findViewById(R.id.rightTv);
        mTitleTv = findViewById(R.id.input_dialog_title);
        mInputDataEd = findViewById(R.id.input_data_ed);
        contentLinView=findViewById(R.id.contentView);
        edLeftTv=findViewById(R.id.edLeftTv);
        edRightTv=findViewById(R.id.edRightTv);
        setEdInputFilter(null);
    }

    public void showTextView(String left,String right){
        edLeftTv.setText(left);
        edRightTv.setText(right);
        edLeftTv.setVisibility(VISIBLE);
        edRightTv.setVisibility(VISIBLE);
    }

    public void setContentView(View view){
        contentLinView.addView(view);
    }

    public InputDialog setConTentText(String text){
        mInputDataEd.setText(text);
        return this;
    }

    public InputDialog initTitle(String title) {
        this.title = title;
        return this;
    }
    public InputDialog initIsClickDimissDialog(boolean isOnBtnClickDialog) {
        this.mIsClickDimissDialog = isOnBtnClickDialog;
        return this;
    }

    public InputDialog initIsShowRedNt(boolean isShowRedNt) {
        this.mIsShowRedNt = isShowRedNt;
        return this;
    }

    public InputDialog initRedHintNt(String redHintNt) {
        this.redHintNt = redHintNt;
        return this;
    }

    public InputDialog initMaxInputDataSize(int maxInputSize) {
        this.maxInputSize = maxInputSize;
        return this;
    }


    public InputDialog initHintText(String hintText) {
        this.hintText = hintText;
        return this;
    }



    public InputDialog setEdInputFilter(InputFilter[] inputFilters) {
        if (inputFilters==null){
            inputFilters= new InputFilter[]{new InputFilterEmoji(),
                    new RegexInputFilter(RegexConstant.Space_Regex, false),
                    new RegexInputFilter(RegexConstant.Enter_Regex, false),
                    new InputFilterEmoji()};
        }
        mInputDataEd.setFilters(inputFilters);
        return this;
    }


    public InputDialog initOnSubMitClickListener(OnSubMitBtnClickListener listener) {
        this.listener = listener;
        return this;
    }




    public void showDialog() {
        leftTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                dismiss();

            }
        });

        rightTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mIsClickDimissDialog){
                    dismiss();
                }
                if (listener != null) {
                    listener.onSubMitBtnClick(getInputData(),InputDialog.this);
                }
            }
        });
        show();
    }

    public void showDialog(String title, String hint, OnSubMitBtnClickListener listener) {
        this.title = title;
        this.hintText = hint;
        this.listener = listener;
        showDialog();
    }

    public void setTitle(String title) {
        this.title = title;
        mTitleTv.setText(title);
    }


    public void setHintText(String hintText) {
        this.hintText = hintText;
        mInputDataEd.setText(hintText);
    }

    public String getInputData() {
        return mInputDataEd.getText().toString();
    }

    @Override
    public void dismiss() {
        View view = getCurrentFocus();
        if (view instanceof EditText) {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(INPUT_METHOD_SERVICE);
            if (getCurrentFocus() != null) {
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            }
        }
        super.dismiss();
    }
}
